package com.hxalex.simulationhandle.ui.permission;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.databinding.ActivityCheckPermissionBinding;
import com.hxalex.simulationhandle.ui.base.BaseActivity;
import com.hxalex.simulationhandle.ui.permission.manger.PermissionManger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

public class CheckPermissionActivity extends BaseActivity implements PermissionManger.onPermissionListener {
private ActivityCheckPermissionBinding mBinding;
private PermissionManger manger;
private String nowApplyPermisson="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      mBinding= DataBindingUtil.setContentView(this, R.layout.activity_check_permission);
      initToolBar();
      intiView();
        EventBus.getDefault().register(this);
    }
    
    

    private void intiView() {
        manger=PermissionManger.getManger(this);
        mBinding.btnAs.setOnClickListener(v->
        {
            manger.jumpSetOpenAccessibilitySetting();
        });
        mBinding.btnSysDl.setOnClickListener(v->
        {
       
            manger.requstOpenSystemAlertDialogPermission();
        });
        mBinding.btnRwSt.setOnClickListener(v->
        {
            nowApplyPermisson=manger.permissionList[0];
            manger.startApplyPermission(this,manger.permissionList[0]);
        });
        mBinding.btnWifiSt.setOnClickListener(v->
        {
            manger.toggleWiFi(this,true);
            Toast.makeText(this,"正在打开wifi",Toast.LENGTH_SHORT).show();
        });
       checkPermissionState();
      manger.setOnPermissionListener(this);
    }
    
    
    private void checkPermissionState()
    {
        HashMap<Integer, Boolean> permissionMap = manger.getHavaPermissionMap();
        if(permissionMap.get(PermissionManger.ISHAVE_ACCESSBILITY_SERVICE))
        {
            mBinding.btnAs.setText("已授权");
            mBinding.btnAs.setTextColor(getResources().getColor(R.color.toolbar_background));
            mBinding.btnAs.setEnabled(false);
        }else
        {
            mBinding.btnAs.setText("打开权限");
            mBinding.btnAs.setTextColor(getResources().getColor(R.color.bg_red));
            mBinding.btnAs.setEnabled(true);
        }

        if(permissionMap.get(PermissionManger.ISHAVE_READ_EXTERNAL_STORAGE))
        {
            mBinding.btnRwSt.setText("已授权");
            mBinding.btnRwSt.setTextColor(getResources().getColor(R.color.toolbar_background));
            mBinding.btnSysDl.setEnabled(false);
        }else
        {
            mBinding.btnRwSt.setText("打开权限");
            mBinding.btnRwSt.setTextColor(getResources().getColor(R.color.bg_red));
            mBinding.btnRwSt.setEnabled(true);
        }

        if(permissionMap.get(PermissionManger.ISHAVE_SYS_dl))
        {
            mBinding.btnSysDl.setText("已授权");
            mBinding.btnSysDl.setTextColor(getResources().getColor(R.color.toolbar_background));
            mBinding.btnSysDl.setEnabled(false);
        }else
        {
            mBinding.btnSysDl.setText("打开权限");
            mBinding.btnSysDl.setTextColor(getResources().getColor(R.color.bg_red));
            mBinding.btnSysDl.setEnabled(true);
        }

        if(permissionMap.get(PermissionManger.ISHAVE_OPEN_WIFI))
        {
            mBinding.btnWifiSt.setText("已开启");
            mBinding.btnWifiSt.setTextColor(getResources().getColor(R.color.toolbar_background));
            mBinding.btnWifiSt.setEnabled(false);
        }else
        {
            mBinding.btnWifiSt.setText("请求打开");
            mBinding.btnWifiSt.setTextColor(getResources().getColor(R.color.bg_red));
            mBinding.btnWifiSt.setEnabled(true);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Log.i("msg","onRequestPermissionsResult");
            if(manger== null) manger=PermissionManger.getManger(this);
            manger.onRequestPermissionsResult(nowApplyPermisson,requestCode,permissions,grantResults);
    }


    @Override
    public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
        return super.shouldShowRequestPermissionRationale(permission);
    }

    @Override
    public void onPermissionCheck() {
        checkPermissionState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        manger.checkAllPermission();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        manger.removeOnPermissionListener(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void checkWifiState(Boolean isOpen)
    {
        manger.checkAllPermission();
        if(isOpen)
        {
            Toast.makeText(this,"wifi已打开",Toast.LENGTH_SHORT).show();
        }else
        {
            Toast.makeText(this,"wifi打开失败，请手动开启",Toast.LENGTH_SHORT).show();
        }

    }
}
